package com.xudow.app.newui.dialog;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.xudow.app.R;

/**
 * 作者：zhouXJ on 2016/4/12 16:07
 * 头像选择提示框
 */
public class HeadChoicePop {

    private PopupWindow popupWindow;
    private static HeadChoicePop headChoicePop;
    private TextView head_pop_cancel_btn;
    private TextView head_pop_tick_p;
    private TextView head_pop_photo;


    public static HeadChoicePop getHeadChoicePopManager() {
        if (headChoicePop == null) {
            headChoicePop = new HeadChoicePop();
        }
        return headChoicePop;
    }

    public void showPopupWindow(View view, final Activity context) {

        View popView = LayoutInflater.from(context).inflate(
                R.layout.head_choice_popup_window, null);

        popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setAnimationStyle(R.style.exit_pop);
        popupWindow.setBackgroundDrawable(null);

        head_pop_cancel_btn = (TextView) popView.findViewById(R.id.head_pop_cancel_btn);
        head_pop_tick_p = (TextView) popView.findViewById(R.id.head_pop_tick_p);
        head_pop_photo = (TextView) popView.findViewById(R.id.head_pop_photo);

        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        WindowManager.LayoutParams lp = context.getWindow().getAttributes(); // 设置窗体透明度
        lp.alpha = 0.3f;
        context.getWindow().setAttributes(lp);

        popupWindow.showAtLocation(view, Gravity.BOTTOM, 0, 0);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                dismissPopWindow(context);
            }
        });



        //打开照相机
        head_pop_tick_p.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (headChoicePopListener != null) {
                    headChoicePopListener.openCamera();
                }
            }
        });

        //打开相册
        head_pop_photo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (headChoicePopListener != null) {
                    headChoicePopListener.openPhoto();
                }
            }
        });

        //取消
        head_pop_cancel_btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                dismissPopWindow(context);
            }
        });

    }

    /**
     * 隐藏pop
     *
     * @param activity
     */
    public void dismissPopWindow(Activity activity) {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
            popupWindow = null;

        }
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes(); // 恢复窗体透明度
        lp.alpha = 1.0f;
        activity.getWindow().setAttributes(lp);
    }

    /**
     * 是否显示
     *
     * @return
     */
    public boolean isShow() {
        if (popupWindow != null && popupWindow.isShowing()) {
            return true;
        } else {
            return false;
        }
    }

    private HeadChoicePopListener headChoicePopListener;

    public void setHeadChoicePopListener(HeadChoicePopListener headChoicePopListener) {
        this.headChoicePopListener = headChoicePopListener;
    }

    public interface HeadChoicePopListener {
        void openCamera();

        void openPhoto();
    }

}
